var App = getApp();
var ApiUrl = App.data.ApiUrl;
var Common = require('../../../utils/common.js');
Page({
  data: {
    type: 0,
    thisone: false,
    choosenid: -1,
    is_useCoupon: true,
    useCoupon: '立即使用'
  },

  onLoad: function (options) {
    getApp().getSaleMode()
    var page = this;

    wx.setNavigationBarTitle({
      title: "我的优惠券"
    })

    if (options) {
      var item, data_json;
      /**
       * options.is_useCoupon，是否展示--“不使用优惠券”--按钮
       * showAllCoupon：详情页点击显示全部的优惠券--不展示按钮
       * order：订单页使用优惠券--展示按钮
       */
      if (options.is_useCoupon == "showAllCoupon") {
        page.setData({
          is_useCoupon: false,
          useCoupon: '可用商品',
        })
      }
      if (options.itemId) {
        item = options.itemId;
        data_json = {
          status: 1,
          itemId: item
        }
      }
      if (options.coupon_array) {
        data_json = {
          status: 1,
          itemInfo: options.coupon_array
        }
      }
      this.getCouponById(data_json);
    }

  },

  // 优惠券详情
  getCouponById: function (data_json) {
    var page = this;
    wx.showLoading({
      title: '加载中...',
    })
    Common.my_request({
      url: 'AppMaster/Coupon/myCoupon',
      data: data_json,
      success: function (res) {
        if (res.data.result == 0) {
          var data = res.data.data;
          if (!data) {
            wx.hideLoading()
           return
          }
          const {list} = data
          if (list) {
            /**将时间戳改成正常时间 */
            for (var i = 0; i < list.length; i++) {
              var start_time = list[i].start_time;
              var stime = page.getDate(start_time, "nomin");
              list[i].start_time = stime;
              var end_time = list[i].end_time;
              var etime = page.getDate(end_time, "nomin");
              list[i].end_time = etime;

              var bind_time = list[i].bind_time;
              var ptime = page.getDate(bind_time, "hasmin");
              list[i].bind_time = ptime;

              var coupon_amount = list[i].coupon_amount;
              coupon_amount = coupon_amount.substring(0, coupon_amount.indexOf("."));
              list[i].coupon_amount = coupon_amount;
            }
            page.setData({
              list: list,
            })
          }
          wx.hideLoading()
        }
      }
    });
  },

  getDate: function (tm, ttype) {
    var tt = new Date(parseInt(tm) * 1000).toLocaleString().replace(/\//g, "-");
    if (ttype == "nomin") {
      tt = tt.substring(0, tt.indexOf(" "));
    }
    return tt;
  },
  /**不使用优惠券 */
  notCoupon: function () {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({
      coupon_bind_id: "",
      cutMoney: 0,
      coupon_name: "不使用优惠券"
    })
    wx.navigateBack();
  },

  /**
   * 优惠券点击跳转到对应产品
   * coupon_type:"2",allow_item_id:"32" 跳到商品详情
   * coupon_type:"1",allow_item_id:"32|64" 跳到商品列表
   * info:优惠券类型_商品id
   */
  goDetail: function (e) {
    var page = this;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面

    /**isBack--true，订单页进来，返回订单详情页
     *       --false，商品详情页进来，跳到列表列表、对应商品页
     */
    var isBack = page.data.is_useCoupon;
    if (isBack) {/**订单页 */
      var info = e.currentTarget.dataset.id.split("_"),
        coupon_type = info[0],
        coupon_bind_id = info[1],
        dk_amount = info[2],
        coupon_amount = info[3],
        coupon_name = info[4];
      if (coupon_type == 1) {/**抵用券 */
        prevPage.setData({
          coupon_bind_id: coupon_bind_id,
          cutMoney: coupon_amount,
          coupon_name: coupon_name
        })
      } else {/**兑换券 */
        prevPage.setData({
          coupon_bind_id: coupon_bind_id,
          cutMoney: dk_amount,
          coupon_name: coupon_name
        })
      }
      wx.navigateBack();
    } else {/**商品详情页 */
      var info = e.currentTarget.id.split("_"),
        dtype = info[0],
        id = info[1];

      if (dtype == 1) {/**抵用券 */
        var intid = parseInt(id);
        if (intid == id && intid) {
          prevPage.setData({
            itemId: id
          })
          wx.navigateBack();
        } else {
          wx.switchTab({
            url: '/pages/product/index/index'
          })
        }
      } else {/**兑换券 */
        prevPage.setData({
          itemId: id
        })
        wx.navigateBack();
      }
    }
  },

  // 详情
  showIntro: function (e) {
    var id = e.currentTarget.id;
    var page = this;
    if (id == page.data.choosenid) {
      page.setData({
        choosenid: -1
      })
    } else {
      page.setData({
        choosenid: id
      })
    }
  },

  // 选项卡点击
  getCoupon: function (e) {
    var id = e.currentTarget.id;
    this.setData({
      type: id
    })
  },

  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  }
})